package com.lam.system.service.impl;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.easyexcel.imp.model.AbstractImportBody;
import com.lam.common.components.easyexcel.imp.model.ImportEntry;
import com.lam.common.components.easyexcel.imp.model.ImportWrapper;
import com.lam.common.components.easyexcel.imp.service.AbstractImportService;
import com.lam.common.components.parameter.ParameterConfigUtil;
import com.lam.common.constant.SystemConstant;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.model.ValidOperType;
import com.lam.system.entity.SysParameter;
import com.lam.system.entity.imp.SysParameterImp;
import com.lam.system.mapper.SysParameterMapper;
import com.lam.system.param.SysParameterParam;
import com.lam.system.service.SysParameterService;

/**
 * 参数配置Service业务层处理
 * 
 * @author admin
 * @date 2022-09-20
 */
@Service
public class SysParameterServiceImpl extends AbstractImportService<SysParameterImp, SysParameter> implements SysParameterService {

    @Autowired
    private SysParameterMapper sysParameterMapper;

    /**
     * 查询参数配置
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysParameter selectById(String id) {

        return sysParameterMapper.selectById(id);
    }

    /**
     * 查询参数配置列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysParameter> selectList(SysParameterParam param) {
    
        QueryWrapper<SysParameter> queryWrapper = new QueryWrapper<>();
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
        	queryWrapper.and(wr->{
        		wr.or().like("name", param.getKeywords());
        		wr.or().like("code", param.getKeywords());
        		wr.or().like("value", param.getKeywords());
        	});
        }
        queryWrapper.like(StringUtils.isNotBlank(param.getNameKey()), "name", param.getNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
        queryWrapper.like(StringUtils.isNotBlank(param.getCodeKey()), "code", param.getCodeKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getCode()), "code", param.getCode());
        queryWrapper.like(StringUtils.isNotBlank(param.getValueKey()), "value", param.getValueKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getValue()), "value", param.getValue());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.eq(Objects.nonNull(param.getType()), "type", param.getType());

        return sysParameterMapper.selectList(queryWrapper);
    }

    /**
     * 新增参数配置
     * 
     * @param sysParameter 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysParameter sysParameter) {
    	int n = sysParameterMapper.insert(sysParameter);
    	ParameterConfigUtil.clear(sysParameter.getCode());
        return n; 
    }

    /**
     * 修改参数配置
     * 
     * @param sysParameter 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysParameter sysParameter) {
    
    	int n = sysParameterMapper.updateById(sysParameter);
    	ParameterConfigUtil.clear(sysParameter.getCode());
        return n; 
    }

    /**
     * 删除参数配置信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	if(StringUtils.isBlank(idStr)) {
            return 0;
    	}
    	int n = 0;
    	SysParameter sysParameter;
    	for (String id : StringUtils.str2List(idStr)) {
    		sysParameter = selectById(id);
            n += sysParameterMapper.deleteById(id);
            if(sysParameter != null) {
            	ParameterConfigUtil.clear(sysParameter.getCode());
    		}
        }
        return n;
    }
    
    @Override
    @Transactional
    public void importData(ImportWrapper<SysParameterImp, SysParameter> wrapper) {
        
    	UniqueDesc uniqueDesc = ValidateUtil.selectUniqueDesc(SystemConstant.UNIQUE_PARAMETER_CODE);
    	importData(wrapper, new AbstractImportBody<SysParameterImp, SysParameter>() {
			@Override
			public SysParameter selectRecord(ImportEntry<SysParameterImp, SysParameter> entry) {
				return ValidateUtil.selectUniqueData(entry.getData(), ValidOperType.ADD, uniqueDesc, SysParameter.class);
			}
			@Override
			public int insertData(SysParameter data) {
				return insert(data);
			}
			@Override
			public int updateData(SysParameter data) {
				return update(data);
			}
			@Override
			public String notUniqueMsg(ImportEntry<SysParameterImp, SysParameter> entry) {
				return MessageFormat.format("编码【{0}】已存在", entry.getData().getCode());
			}
    	});
    }
    
}
